﻿using System;
using System.Collections.Generic;

namespace SenileCoderMvc.UnitTests
{
    /// <summary>
    /// Helper class to compare 2 values are within a certain range.
    /// </summary>
    /// <remarks>
    /// found at http://quickduck.com/blog/2009/09/07/unit-testing-datetime-now/
    /// updated by christopher.loggins@gmail.com
    /// </remarks>
    public class DateComparer : IComparer<DateTime>
    {
        public TimeSpan MarginOfError { get; private set; }

        public DateComparer(TimeSpan marginOfError)
        {
            MarginOfError = marginOfError;
        }

        public int Compare(DateTime expected, DateTime actual)
        {
            var margin = expected - actual;
            return margin <= MarginOfError ? 0 : expected.CompareTo(actual);
        }
    }
}
